package com.itheima.d10_ziXiZuoYe;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //1.
        ArrayList<Integer> list = new ArrayList<Integer>();

        //2.
        Class<?> clazz = list.getClass();

        //3.
        Method addMethod = clazz.getMethod("add", Object.class);

        //4.
        addMethod.invoke(list, "这是一个字符串");
        addMethod.invoke(list, "另一个字符串");

        //5.
        System.out.println("ArrayList中的数据：");
        for (int i = 0; i < list.size(); i++) {
            Object obj = list.get(i);
            System.out.println("索引" + i + ":" + obj + "(类型:" + obj.getClass().getSimpleName() + ")");
        }

        //6.
        list.add(100);
        System.out.println("\n添加Integer后的数据：");
        for (int i = 0; i < list.size(); i++) {
            Object obj = list.get(i);
            System.out.println("索引" + i + ":" + obj + "(类型:" + obj.getClass().getSimpleName() + ")");
        }

    }
}
